package Server;

import Data.CommonSentenceData;
import Data.event;
import MyInterfaces.MassageType;

import java.awt.*;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class CommonSentenceOperation {

    static String url = "jdbc:mysql://127.0.0.1:3306/nox?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
    static String user = "root";
    static String password = "Mi20030610.";

    public boolean add(CommonSentenceData commonSentenceData) throws ClassNotFoundException, SQLException, IOException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        String sql="insert into common_sentence_list (userid,sentence) values (?,?);";

        PreparedStatement operation=Conn.prepareStatement(sql);
        operation.setString(1,commonSentenceData.getUserid());
        operation.setString(2,commonSentenceData.getCommomsentence());

        int k=operation.executeUpdate();
        if(k<=0)
        {
            event returnmassage=new event();
            returnmassage.setType(MassageType.Add_Common_Sentence_Fail);
            returnmassage.setT(commonSentenceData);

            Socket socket=ManageClientConnectionServerThread.getSocketByUserid(commonSentenceData.getUserid());
            if(socket!=null)
            {
                ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                writer.writeObject(returnmassage);
                writer.flush();
            }
            return false;
        }

        //添加成功，给ta发送一个成功的消息
        event returnmassage=new event();
        returnmassage.setType(MassageType.Add_Common_Sentence_Success);
        returnmassage.setT(commonSentenceData);

        Socket socket=ManageClientConnectionServerThread.getSocketByUserid(commonSentenceData.getUserid());
        if(socket!=null)
        {
            ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
            writer.writeObject(returnmassage);
            writer.flush();
        }
        return true;
    }

    public boolean delete(CommonSentenceData commonSentenceData) throws ClassNotFoundException, SQLException, IOException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        String sql="delete from common_sentence_list where userid=? and sentence=?;";

        PreparedStatement operation=Conn.prepareStatement(sql);
        operation.setString(1,commonSentenceData.getUserid());
        operation.setString(2,commonSentenceData.getCommomsentence());

        int k=operation.executeUpdate();
        if(k<=0)
        {
            event returnmassage=new event();
            returnmassage.setType(MassageType.Delete_Common_Sentence_Fail);
            returnmassage.setT(commonSentenceData);

            Socket socket=ManageClientConnectionServerThread.getSocketByUserid(commonSentenceData.getUserid());
            if(socket!=null)
            {
                ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                writer.writeObject(returnmassage);
                writer.flush();
            }
            return false;
        }

        //添加成功，给ta发送一个成功的消息
        event returnmassage=new event();
        returnmassage.setType(MassageType.Delete_Common_Sentence_Success);
        returnmassage.setT(commonSentenceData);

        Socket socket=ManageClientConnectionServerThread.getSocketByUserid(commonSentenceData.getUserid());
        if(socket!=null)
        {
            ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
            writer.writeObject(returnmassage);
            writer.flush();
        }
        return true;
    }
}
